<?php 
    session_start();
    if(!isset($_SESSION['islogined'])||@$_SESSION['idposition']==3)
        header("location:./Login.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="app.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

</head>
<body>
    <?php include 'header1.php'; ?>
    <div class="container">
        <ol class="breadcrumb path-nav">
            <li><a href="./Home.php">Home</a></li>
            <li class="active">Task Management</li>
        </ol>
    </div>
    <div class="container">
        <div class="col-md-3 col-md-offset-9 ">
            <div class="input-group">
                <input type="text" class="form-control" placeholder="Task Name">       
                <span class="glyphicon glyphicon-search input-group-addon"></span>         
            </div>                     
        </div>            
    </div>
    <div class="container list-container ">
        <div class="col-md-3">            
                <ul class="list-group">
                <li id='all_task_li' class="list-group-item active" data-name="all">All Tasks</li>
                <ul class="all-task">
                   <li class="list-group-item all-task-item" data-name="alcted"> Allocated Tasks</li>
                   <li class="list-group-item all-task-item" data-name="unalcted"> Unallocated Tasks</li>
                   <li class="list-group-item all-task-item" data-name="finished"> Finished Tasks</li>
                   <li class="list-group-item all-task-item" data-name="unfinished"> Unfinished Tasks</li> 
                </ul>
                <li class="list-group-item" data-name="crt_task">Create Task</li>
                <li class="list-group-item" data-name="alc_task">Allocate Task</li>
                </ul>          
        </div>
        <div class="col-md-9 ">
            <?php
                include'infor_change.php';
                $sql="SELECT *FROM task 
                WHERE taskID not in (SELECT subTaskID FROM task_hierarchy)";
                show_title();
                show_task($sql);
            ?>
        </div>
    </div>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
<script src="jquery-3.0.0.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>
    $(function(){
        bottomclc();
        boxJump();
        $('#all_task_li').click(function(){
            $('.all-task').toggle();
        });
        $('.list-group-item').click(function(){
            $('.list-group-item').removeClass('active');
            $(this).addClass('active');
            var type=$(this).data('name');
            $.post("infor_change.php",{
               task_request_type:type
           },function (data){
               $('.col-md-9').html(data);
               bottomclc();
               boxJump();
           })
        });
        $(document).on('click',"#delete_task",function(){
            var taskid=$(this).data('id');
            $.post("infor_change.php",{
                taskid:taskid,
                delete_task:1
            },function(){
                $('.l_'+taskid).hide();
            });
        })
        $(document).on('change','#surpertask_choose',function(){
            var supertask=$(this).val();
            $.post("infor_change.php",{
                creat_choose:1,
                supertask:supertask
            },function(data){
                $('.col-md-9').html(data);
            });
        });
        $(document).on('change','#template_choose',function(){
            var templateid=$(this).val();
            var supertask=$('#surpertask_choose').val();
            $.post("infor_change.php",{
                creat_choose:2,
                supertask:supertask,
                templateid:templateid
            },function(data){
                $('.col-md-9').html(data);
            });
        });
        $(document).on('click','#alc_cfm_btn',function(){
            var taskid=$('#taskid').val();
            var username=$('#username').val();
            $.post("infor_change.php",{
                insert_alc_task:1,
                taskid:taskid,
                username:username
            },function(data){
                $('.fail-modify').remove();
                $('.success-modify').remove();
                $('.form-header').before(data);
            });
        });
        $(document).on('click','#creat_submit_btn',function(){
            //$('.form-header').before("<div class='success-modify'>success!</div>");
            $('.fail-modify').remove();
            $('.success-modify').remove();
            if($('#template_choose').val()=='none'){//when the user doesn't choose the template
                $('.form-header').before("<div class='fail-modify'>Please choose a template!</div>");
            }
            else if($('#taskname_input').val()==''){
                $('.form-header').before("<div class='fail-modify'>Please input the taskname!</div>");
            }
            else if($('#unit_num').length>0 && $('#unit_num').val()==''){
                $('.form-header').before("<div class='fail-modify'>You have input value is none!</div>");
            }
            else if($('#acl_wlus').length>0 && $('#acl_wlus').val()==''){
                $('.form-header').before("<div class='fail-modify'>You have input value is none!</div>");
            }
            else{
                var taskid=$('#surpertask_choose').val();
                var templateid=$('#template_choose').val();
                var taskname=$('#taskname_input').val();
                var unit_num='none';
                var acl_wlus='none';
                var modulecode=$('#module_choose').val();
                if($('#unit_num').length>0){
                    unit_num=$('#unit_num').val();
                }
                if($('#acl_wlus').length>0){
                    acl_wlus=$('#acl_wlus').val();
                }
                var note=$('#note').val();
                $.post('infor_change.php',{
                    task_infor_creat:1,
                    taskid:taskid,
                    templateid:templateid,
                    taskname:taskname,
                    modulecode:modulecode,
                    unit_num:unit_num,
                    acl_wlus:acl_wlus,
                    note:note
                },function(data){
                    $('.form-header').before(data);
                });
            }
        });
    });
    function bottomclc(){
        $(".subtemplete").unbind('click').bind('click',function(){
            if($(this).hasClass('glyphicon-triangle-bottom')){
                $(this).removeClass('glyphicon-triangle-bottom');
                $(this).addClass('glyphicon-triangle-left');
            }
            else{
                $(this).removeClass('glyphicon-triangle-left');
                $(this).addClass('glyphicon-triangle-bottom');
            }
            var id=$(this).data('id');
            $("."+id).toggle();
        });
    }
    function boxJump(){
        $(".jm-box").click(function(){
            var id=$(this).data('id');
            var type=$(this).data('type');
            var str=type+"_"+id;
            if(type=='D'){
                $.post("infor_change.php",{
                    taskid:id,
                    deleteTaskshow:1
                },function(data){
                    $('#myModal').html(data);                
                    $('#myModal').modal();
                });
            }else{
                $.get("TaskRequest.php?id="+str,null,function(data){
                    $('#myModal').html(data);                
                    $('#myModal').modal();
                    formdispose();
                })
            }    
        });
    }
    function formdispose(){
    $("#update_btn").click(function(){
           var f_type="update_task";
           var f_taskID=$("#taskID").val();
           if($("#wlus").length>0){
               var f_wlus=$("#wlus").val();
           }
           else{
               f_wlus=null;
           }
           var f_notes=$("#notes").val();
           $.post("infor_change.php",{
               type:f_type,
               taskID:f_taskID,
               wlus:f_wlus,
               notes:f_notes
           },function (data){
               $('.success-coin').remove();
               $('.l_'+f_taskID).html(data);
               boxJump();
               bottomclc();
           })
       });
    }
</script>
</body>
</html>